class Context {
    constructor() {
        this.state = new ConcreteStateA(this)
    }
    setState (state) {
        this.state = state
    }
    request () {
        this.state.handle()
    }
}

class State {
    constructor(context) {
        this.context = context
    }
    handle () {
        throw new Error('抽象状态')
    }
}

class ConcreteStateA extends State {
    handle () {
        console.log('StateA')
        this.context.setState(new ConcreteStateB(this.context))
    }
}

class ConcreteStateB extends State {
    handle () {
        console.log('StateB')
        this.context.setState(new ConcreteStateA(this.context))
    }
}

const context = new Context()
context.request()
context.request()
context.request()